package restaurant.selector.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class AddUserShell extends Dialog {

	private String username;
	
	public AddUserShell(Shell shell ){
		this(shell, SWT.NONE);
	}
	
	public AddUserShell(Shell shell, int style ){
		
		super(shell, style);
	}
	
	public void open () {
        Shell parent = getParent();
        final Shell shell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
        shell.setText("Add New User");

        GridLayout gridLayout = new GridLayout();
        gridLayout.numColumns = 1;
        shell.setLayout(gridLayout);
        
		GridData data = new GridData();
		Label label = new Label(shell, SWT.NONE);
		label.setText("Please enter user : ");
		data.widthHint = 200;
		label.setLayoutData(data);
		
		final Text text = new Text(shell, SWT.BORDER);
		text.setTextLimit(20);
		data = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
		text.setLayoutData(data);
        
		Composite buttons = new Composite(shell, SWT.NONE);
        GridLayout buttonLayout = new GridLayout();
        buttonLayout.numColumns = 2;
        buttons.setLayout(buttonLayout);
        
		Button ok = new Button(buttons, SWT.PUSH);
		ok.setText("OK");
		ok.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				username = text.getText();
				shell.close();
			}
		});
		
		Button cancel = new Button(buttons, SWT.PUSH);
		cancel.setText("Cancel");
		cancel.addListener(SWT.Selection, new Listener(){
			public void handleEvent(Event event) {
				shell.close();
			}
		});
		
		shell.pack();
        shell.open();
        Display display = parent.getDisplay();
        while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) display.sleep();
        }
	}

	public String getUsername() {
		return username;
	}
}
